layui.define(['table', 'form', 'element'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form, element = layui.element;

  var userId = $('input[name="id"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/user/wx_user/list.data'
        ,where:{userId:userId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,size:'lg'
        ,cols: [[
            {field: 'appId', title: '微信应用',templet:function (record){
                    return '['+record.appTypeName+']' + record.appId;
                }}
            ,{field: 'openId', title: '微信openId'}
            ,{field: 'faceImg', title: '头像',templet:function (record){
                if(record.faceImg) {
                    return '<img src="' + record.faceImg + '">';
                }else{
                    return '--';
                }
                }}
            ,{field: 'nickname', title: '微信昵称'}
            ,{field: 'name', title:'微信真实姓名',templet:function (record){
                if(!record.phone){record.phone = '--'};
                if(!record.name){record.phone = '--'};
                return record.name + '('+record.phone+')';
                }}
            ,{field: 'createDate', title: '创建时间'}
        ]]
        ,page: false
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });

    table.render({
        elem: '#LAY-table-place'
        ,url: layui.setter.webRoot + '/user/place/list.data'
        ,where:{userId:userId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'id', title: '网点ID'}
            ,{field: 'name', title: '网点名称'}
            ,{field: 'roleTypeName', title: '角色', templet: function (record){
                    return '代理';
            }}
            ,{field: 'incomeRatioContent', title: '分成',templet: function (record){
                    return '100%';
            }}
            ,{field: 'typeNames', title: '网点类型'}
            ,{field: 'areaName', title: '所在区域'}
            ,{field: 'stateName', title:'状态'}
            ,{field: 'createDate', title: '创建时间'}
        ]]
        ,page: true
        ,limit: 30
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    table.render({
        elem: '#LAY-table-relevance-place'
        ,url: layui.setter.webRoot + '/user/relevance_place/list.data'
        ,where:{userId:userId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'id', title: '网点ID', templet: function (record){
                    return record.place.id;
                }}
            ,{field: 'name', title: '网点名称', templet: function (record){
                    return record.place.name;
                }}
            ,{field: 'roleTypeName', title: '角色'}
            ,{field: 'incomeRatioContent', title: '分成',templet: function (record){
                    return record.incomeRatioInfo;
                }}
            ,{field: 'typeNames', title: '网点类型', templet: function (record){
                    return record.place.typeNames;
                }}
            ,{field: 'placeUserName', title: '网点所属人', templet: function (record){
                    return record.placeUserName;
                }}
            ,{field: 'areaName', title: '所在区域', templet: function (record){
                    return record.place.areaName;
                }}
            ,{field: 'stateName', title:'状态', templet: function (record){
                    return record.place.stateName;
                }}
            ,{field: 'createDate', title: '加入时间', templet: function (record){
                    return record.createDate;
                }}
        ]]
        ,page: true
        ,limit: 30
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    element.on('tab(tab-info)', function (data) {
        if(data.index == 0) {
            table.reload('LAY-table')
        }else if(data.index == 2) {
            table.reload('LAY-table-place')
        }else if(data.index == 3) {
            table.reload('LAY-table-relevance-place')
        }
    });

    //事件
    var active = {
        updatePwd:function (){
            layer.open({
                type: 2
                ,title: '修改密码'
                ,content: layui.setter.webRoot + '/user/pwd/update.html?userId='+userId
                ,maxmin: true
                ,area: ['500px', '450px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/user/pwd/update.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.msg('修改密码成功',{icon: 1});
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
    }

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

  exports('user_info', {})
});